<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="智能合约开发笔记"><meta name="keywords" content="区块链"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>智能合约开发笔记 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Remix"><span class="toc-number">1.</span> <span class="toc-text">Remix</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#MetaMask"><span class="toc-number">2.</span> <span class="toc-text">MetaMask</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#ganache"><span class="toc-number">3.</span> <span class="toc-text">ganache</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Truffle"><span class="toc-number">4.</span> <span class="toc-text">Truffle</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#浏览器端和智能合约进行交互"><span class="toc-number">5.</span> <span class="toc-text">浏览器端和智能合约进行交互</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IPFS"><span class="toc-number">6.</span> <span class="toc-text">IPFS</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">智能合约开发笔记</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-09</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/%E5%8C%BA%E5%9D%97%E9%93%BE/">区块链</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>记录智能合约开发的环境配置及各个软件的功能。</p>
<a id="more"></a>

<h3 id="Remix"><a href="#Remix" class="headerlink" title="Remix"></a>Remix</h3><p>一个在线的智能合约开发环境。</p>
<h3 id="MetaMask"><a href="#MetaMask" class="headerlink" title="MetaMask"></a>MetaMask</h3><p><a href="https://metamask.io/" target="_blank" rel="noopener">https://metamask.io/</a><br>这是一个 chrome 插件，管理以太坊钱包。用这个插件，你可以把你以太坊的公有和私有的钱包导入进来进行关联，并且使用 Remix 开发合约时，可以直接连通，使用钱包中的信息。</p>
<h3 id="ganache"><a href="#ganache" class="headerlink" title="ganache"></a>ganache</h3><p><a href="https://www.trufflesuite.com/ganache" target="_blank" rel="noopener">https://www.trufflesuite.com/ganache</a><br>快速启动个人 Ethereum 区块链，您可以使用它来运行测试、执行命令和检查状态，同时控制链的操作方式。</p>
<p>安装完后，启动后，它会在<code>http://127.0.0.1:7545</code>上启动一个服务，并生成几个私有合约的账号，用这些可以方便我们进行测试。</p>
<p>启动后，就可以使用<code>MetaMask</code>连接。</p>
<ul>
<li>把 metaMask 的网络选择”自定义 RPC”</li>
<li>输入名称，url 中输入<code>http://127.0.0.1:7545</code></li>
</ul>
<p>上面两步就连上了<code>metamask</code>启动的服务。把<code>metamask</code>上面的账户导入到 metamask 中，就可以看到对应账户的以太币。<br>复制账户的私钥，然后在 metamask 中导入即可</p>
<p>和<code>metamask</code>连通后，刷新<code>remix</code>，<code>remix</code>可以自动读取<code>metamask</code>的账户信息，可以用来进行智能合约的测试。</p>
<h3 id="Truffle"><a href="#Truffle" class="headerlink" title="Truffle"></a>Truffle</h3><p><a href="https://www.trufflesuite.com/" target="_blank" rel="noopener">https://www.trufflesuite.com/</a><br>这个是以太坊的开发套件，大家都喜欢称为瑞士军刀套件。有了这个就可以让智能合约的<strong>编译</strong>和<strong>部署</strong>就可以变的自动化。<br>在使用 remix 时，编译和部署都是在浏览器中自动进行的。我们自己开发中，合约写完后，需要把编译成 bat code 和 ibi 接口，ibi 是一个 json，是用来部署和 web3 来做接口。使用 truffle 可以让上面的流程自动化。<br>全局安装</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g truffle</span><br></pre></td></tr></table></figure>

<p>使用下面的命令可以下载官网提供的宠物领养的例子</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">truffle unbox pet-shop</span><br></pre></td></tr></table></figure>

<ul>
<li><code>contracts/</code>: 合约都放在这个文件夹中</li>
<li><code>migrations/</code>:部署文件放在这里</li>
<li><code>truffle-config.js</code>: Truffle configuration file</li>
</ul>
<p>使用如下命令编译合约</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">truffle compile</span><br></pre></td></tr></table></figure>

<p>部署合约</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">truffle migrate --reset</span><br></pre></td></tr></table></figure>

<p>也可以加–reset 重启服务<br>部署成功后，在<code>ganache</code>中就可以看到新部署的合约账户了。</p>
<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">truffle test</span><br></pre></td></tr></table></figure>

<p>上面会测试所有 test 文件夹下的测试文件。注意：需要在项目中安装 truffle，即使全局已安装。</p>
<h3 id="浏览器端和智能合约进行交互"><a href="#浏览器端和智能合约进行交互" class="headerlink" title="浏览器端和智能合约进行交互"></a>浏览器端和智能合约进行交互</h3><p>上面的所有的操作，就把只能合约开发的服务端配置做完了，写的合约也能编译部署了，接下来就可以通过客户端来进行操作了。<br>前端操作智能合约需要两个库</p>
<ul>
<li>web3</li>
<li>truffle-contract //这是 truffle 在前端又包了一层</li>
</ul>
<p>创建一个 react 项目，把上面的合约服务器代码放到 src 下面，然后退回到前端代码的根目录，安装下面的两个库，因为这两个库是给前端开发使用的。<br>在项目中安装这两个库</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i web3 truffle-contract --save</span><br></pre></td></tr></table></figure>

<blockquote>
<p>浏览器安装了<code>metaMask</code>，浏览器中就有了 web3 实例。</p>
</blockquote>
<h3 id="IPFS"><a href="#IPFS" class="headerlink" title="IPFS"></a>IPFS</h3><blockquote>
<p>“IPFS 全称是 Inter Planetary File System,直接翻译的话叫做“星际文件系统”,它由 Protocol Lab 提出,是一种点对点(P2P)的分布式文件系统。不过虽然说它是文件系统,是一种用来存储数据的技术,但更确切的说 IPFS 是一种传输协议。”</p>
</blockquote>
<p>ipfs 可以通过多种方式操作，也有 nodejs 的版本。<a href="https://github.com/ipfs/js-ipfs" target="_blank" rel="noopener">https://github.com/ipfs/js-ipfs</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install ipfs --global</span><br></pre></td></tr></table></figure>

<p>全局安装以后，可以直接在命令窗口中进行操作。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jsipfs --help</span><br></pre></td></tr></table></figure>

<p>就可以直接查看它的所有命令。下面命令是简单演示，上传一个本地文件，并查看和同步</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jsipfs init</span><br><span class="line">jsipfs add file.html</span><br><span class="line">jsipfs cat &lt;hash&gt;</span><br></pre></td></tr></table></figure>

<p><code>&lt;hash&gt;</code> 是上传的时候生成的 hash 值，通过这个就可以直接在网页或者命令查看上传的内容。</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/09/%E6%99%BA%E8%83%BD%E5%90%88%E7%BA%A6%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0/">http://blog.wangpengpeng.site/2020/01/09/%E6%99%BA%E8%83%BD%E5%90%88%E7%BA%A6%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E5%8C%BA%E5%9D%97%E9%93%BE/">区块链</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/09/%E5%89%8D%E7%AB%AF%E7%9F%A5%E8%AF%86%E7%82%B9%E4%B8%B2%E8%AE%B2-2/"><i class="fa fa-chevron-left">  </i><span>前端知识点串讲-2</span></a></div><div class="next-post pull-right"><a href="/2020/01/09/%E8%BE%93%E5%85%A5%E6%A1%86%E7%A6%81%E6%AD%A2%E8%BE%93%E5%85%A5%E8%A1%A8%E6%83%85%E5%92%8C%E7%89%B9%E6%AE%8A%E7%AC%A6%E5%8F%B7/"><span>输入框禁止输入表情和特殊符号</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>